a = 12
a[1] 12
A single number is a scalar
\[a = 12\]
We already talked about vectors in R.
How would you define them?
A collection of concatenated elements
Column vector
\[ \overrightarrow b = \begin{bmatrix} 12 \\ 14 \\ 15 \end{bmatrix} \]
Row vector
\[ \overrightarrow c = \begin{bmatrix} 12 & 14 & 15 \end{bmatrix} \]
\[\overrightarrow d = \begin{bmatrix} 12 & 7 & -2 & 3 & -1 \end{bmatrix}\]
\[\sum_{i=1}^3 d_i\]
\[\overrightarrow d = \begin{bmatrix} 12 & 7 & -2 & 3 & -1 \end{bmatrix}\]
\[\sum_{i=1}^3 d_i = 12+7+(-2)\]
\[\overrightarrow d = \begin{bmatrix} 12 & 7 & -2 & 3 & -1 \end{bmatrix}\]
\[ \sum_{i=1}^n d_i \]
\[\overrightarrow d = \begin{bmatrix} 12 & 7 & -2 & 3 & -1 \end{bmatrix}\]
\[ \sum_{i=1}^n d_i = 12 + 7 + (-2) + 3 + (-1) \]
\[\overrightarrow d = \begin{bmatrix} 12 & 7 & -2 & 3 & 1 \end{bmatrix}\]
\[\prod_{i=1}^n d_i\]
\[\overrightarrow d = \begin{bmatrix} 12 & 7 & -2 & 3 & 1 \end{bmatrix}\]
\[\prod_{i=1}^n d_i = 12 \cdot 7 \cdot (-2) \cdot 3 \cdot (-1)\]
\[\overrightarrow d = \begin{bmatrix} 12 & 7 & -2 & 3 & 1 \end{bmatrix}\]
Practice: Write and calculate the product of the first three elements of \(\overrightarrow{d}\)
\[A = \begin{bmatrix} 12 & 14 & 15 \\ 115 & 22 & 127 \\ 193 & 29 & 219 \end{bmatrix}\]
Quantitative data comes in rectangular format
But in R, matrices and data frames are different!
Being good at matrix algebra makes you good at cleaning data
Follow technical details in statistics (e.g. OLS regression)
Write more efficient code
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
\[ B= \begin{bmatrix} b_{11} & b_{12} & b_{13} & \ldots & b_{1n} \\ b_{21} & b_{22} & b_{23} & \ldots & b_{2n} \\ \vdots & \vdots & \vdots & \ldots & \vdots \\ b_{m1} & b_{m2} & b_{m3} & \ldots & b_{mn} \end{bmatrix} \]
\[ B= \begin{bmatrix} b_{11} & b_{12} & b_{13} & \ldots & b_{1n} \\ b_{21} & b_{22} & b_{23} & \ldots & b_{2n} \\ \vdots & \vdots & \vdots & \ldots & \vdots \\ b_{m1} & b_{m2} & b_{m3} & \ldots & b_{mn} \end{bmatrix} \]
Thinking about matrices of arbitrary dimensions is tricky but useful!
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
Practice: Write code to find the index corresponding to number 12
\[A \pm B=C\]
\[c_{ij}=a_{ij} \pm b_{ij} \text{ }\forall i,j\]
Add or subtract each corresponding element
Need \(A\) and \(B\) to have exactly the same dimensions
aka being of equal order or having equal size
\[\begin{bmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33} \end{bmatrix} \pm \begin{bmatrix} b_{11} & b_{12} & b_{13}\\ b_{21} & b_{22} & b_{23}\\ b_{31} & b_{32} & b_{33} \end{bmatrix}\] \[=\] \[\begin{bmatrix} a_{11}\pm b_{11} & a_{12}\pm b_{12} & a_{13}\pm b_{13}\\ a_{21}\pm b_{21} & a_{22}\pm b_{22} & a_{23}\pm b_{23}\\ a_{31}\pm b_{31} & a_{32}\pm b_{32} & a_{33}\pm b_{33} \end{bmatrix}\]
\[A= \begin{bmatrix} 3 & -1 & 2 \\ 9 & 4 & 6 \end{bmatrix}\]
\[B= \begin{bmatrix} 5 & 2 & 0 \\ 9 & 3 & 4 \end{bmatrix}\]
\[A= \begin{bmatrix} 3 & -1 & 2 \\ 9 & 4 & 6 \end{bmatrix}\]
\[A= \begin{bmatrix} 3 & -1 & 2 \\ 9 & 4 & 6 \end{bmatrix}\]
Do one with code and the other by hand.
1) Calculate \(A + B\)
\[A= \begin{bmatrix} 1 & 0 \\ -2 & -1 \end{bmatrix}\]
\[B = \begin{bmatrix}
5 & 1 \\
2 & -1
\end{bmatrix}\]
2) Calculate \(A - B\)
\[A= \begin{bmatrix} 6 & -2 & 8 & 12 \\ 4 & 42 & 8 & -6 \end{bmatrix}\]
\[B = \begin{bmatrix} 18 & 42 & 3 & 7 \\ 0 & -42 & 15 & 4 \end{bmatrix}\]
\[A = \begin{bmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33} \end{bmatrix}\] \[cA = \begin{bmatrix} ca_{11} & ca_{12} & ca_{13}\\ ca_{21} & ca_{22} & ca_{23}\\ ca_{31} & ca_{32} & ca_{33} \end{bmatrix}\]
Calculate \(2\times A\) and \(-3 \times B\).
\[A= \begin{bmatrix} 1 & 4 & 8 \\ 0 & -1 & 3 \end{bmatrix}\] \[ B = \begin{bmatrix} -15 & 1 & 5 \\ 2 & -42 & 0 \\ 7 & 1 & 6 \end{bmatrix}\]
Do one by hand, one in code
Matrices must be conformable
For \(A \times B\) we need \(A_{i \times k}\) and \(B_{k \times j}\)
The result will have \(i \times j\) dimensions
Order matters! \(A \times B \neq B \times A\) even if both operations are conformable
Many errors in data analysis happen because we try to multiply matrices that are not conformable
Multiply each row by each column, summing up each pair of multiplied terms (dot product)
The element in position \(ij\) is the sum of the products of elements in the \(i\)th row of the first matrix (\(A\)) and the corresponding elements in the \(j\)th column of the second matrix (\(B\)). \[c_{ij}=\sum_{k=1}^n a_{ik}b_{kj}\]
Furniture company construction costs ($)
| Chair | Sofa | |
|---|---|---|
| Wood | 100 | 150 |
| Cloth | 270 | 420 |
| Feathers | 130 | 195 |
\[C = \begin{bmatrix} 100 & 150\\ 270 & 420\\ 130 & 195 \end{bmatrix}\]
Monthly production
| Product | Quantity |
|---|---|
| Chair | 45 |
| Sofa | 30 |
\[Q = \begin{bmatrix} 45 \\ 30 \end{bmatrix}\]
Total expenditure
\[ E = CQ = \begin{bmatrix} 100 & 150\\ 270 & 420\\ 130 & 195 \end{bmatrix} \begin{bmatrix} 45 \\ 30 \end{bmatrix}\]
| Chair | Sofa | |
|---|---|---|
| Wood | 100 | 150 |
| Cloth | 270 | 420 |
| Feathers | 130 | 195 |
| Product | Quantity |
|---|---|
| Chair | 45 |
| Sofa | 30 |
Total expenditure
\[ E = CQ = \begin{bmatrix} 100 & 150\\ 270 & 420\\ 130 & 195 \end{bmatrix} \begin{bmatrix} 45 \\ 30 \end{bmatrix}\]
\[ = \begin{bmatrix} (100)(45) + (150)(30) \\ (270)(45) + (420)(30) \\ (130)(45) + (195)(30) \end{bmatrix} = \begin{bmatrix} 9,000 \\ 24,750 \\ 11,700 \end{bmatrix} \]
Check in R with the %*% operator to see if these are conformable
\[ \begin{aligned} B_{4 \times 1}= \begin{bmatrix} 2 \\ 3\\ 4\\ 1 \end{bmatrix} M_{3 \times 3} = \begin{bmatrix} 1 & 0 & 2\\ 1 & 2 & 4\\ 2 & 3 & 2 \end{bmatrix} L_{2 \times 3} = \begin{bmatrix} 6 & 5 & -1\\ 1 & 4 & 3 \end{bmatrix} \end{aligned} \]
What happens when they are not?
Addition and subtraction:
Multiplication:
\(AB \neq BA\)
If \(AB = BA\) we say they commute
\(A(BC) = (AB)C\)
\(A(B+C) = AB + AC\)
\[ A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \]
\(m=n\)
Diagonal \(d(A)=\{1,5,9\}\)
Trace \(tr(A) = 1+5+9 = 15\)
\[ D = \begin{bmatrix} 4 & 0 & 0 \\ 0 & 5 & 0 \\ 0 & 0 & 6 \end{bmatrix} \]
\[ S = \begin{bmatrix} 7 & 0 & 0 \\ 0 & 7 & 0 \\ 0 & 0 & 7 \end{bmatrix} \]
\[ I = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} \]
\[J = \begin{bmatrix} 4 & 5\\ 3 & 0\\ 7 & -2 \end{bmatrix} \quad J' = J^T = \begin{bmatrix} 4 & 3 & 7 \\ 5 & 0 & -2 \end{bmatrix}\]
\[A × A^{-1} = I\]
The inverse of \(A\) is \(A^{-1}\) iff
\[AA^{-1} = A^{-1}A = I\]
[,1] [,2] [,3]
[1,] 3 2 5
[2,] 2 3 2
[3,] 5 2 4
\[ A \div B = A \times B^{-1} \]
This is true iff \(B\) is invertible
If \(B\) is not invertible, then answer is not unique
Because matrix multiplication is not commutative
\[\widehat{\beta} = \underbrace{(X'X)^{-1}X'Y}_\text{"X prime X inverse, X prime Y"}\]